import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_treeview/tree_view.dart';

import 'package:wis_flutter/bottomBar.dart';     // 底部导航条



// 组织树
class TreeviewPage extends StatefulWidget {

  final double height; // 高度
  final List children;     // 数据
  final dynamic onClick;        // 点击事件
  final dynamic onExpansionChanged;    // 展开事件



  TreeviewPage({
    Key key,
    this.height,
    this.children,
    this.onClick,
    this.onExpansionChanged,

  }) : super(key: key);  

  @override
  _TreeviewPageState createState() => _TreeviewPageState();
}

class _TreeviewPageState extends State<TreeviewPage> {

  TreeViewController _treeViewController;

  @override
  void initState() {
    super.initState();


    // 初始化 树
    _treeViewController = TreeViewController(
      // children: _nodes,
      // selectedKey: "SD",
    );

    // 设置数据
    // if( widget.children !=null ){
    //   setState(() {
    //     _treeViewController= _treeViewController.loadJSON(json: jsonEncode(widget.children) );
    //   });
    // }
   


  }


    @override
    Widget build(BuildContext context) {

      TreeViewTheme _treeViewTheme = TreeViewTheme(
        expanderTheme: ExpanderThemeData(
          type: ExpanderType.caret,
          modifier: ExpanderModifier.none,
          position: ExpanderPosition.end,
          color: Colors.grey.shade800,
          size: 20,
        ),
        labelStyle: TextStyle(
          fontSize: 14,
          letterSpacing: 0.1,
          
        ),
        parentLabelStyle: TextStyle(
          fontSize: 14,
          letterSpacing: 0.1,
          fontWeight: FontWeight.w800,
          color: Colors.blue[700],
          
        ),
        iconTheme: IconThemeData(
          size: 14,
          color: Colors.grey.shade800,
        ),
        colorScheme: Theme.of(context).brightness == Brightness.light
            ? ColorScheme.light(
                primary: Colors.blue.shade50,
                onPrimary: Colors.grey.shade900,
                background: Colors.transparent,
                onBackground: Colors.black,
              )
            : ColorScheme.dark(
                primary: Colors.black26,
                onPrimary: Colors.white,
                background: Colors.transparent,
                onBackground: Colors.white70,
              ),
      );


      return Container(
        height: widget.height,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(4.0)),
            border:Border.all(
              width:1,
              color:Color(0xffe5e5e5) 
              // color: Colors.grey[500]
            )
        ),        
        // decoration: BoxDecoration(
        //   color: Colors.white,
        //   borderRadius: BorderRadius.circular(4),
        // ),
        padding: EdgeInsets.all(8),
        child: TreeView(
          // controller: _treeViewController,
          controller: _treeViewController.loadJSON(json: jsonEncode(widget.children) ),
          allowParentSelect: true,
          supportParentDoubleTap: true,
          onExpansionChanged: (key, expanded){
            // 回调
            if( widget.onExpansionChanged !=null ){
              widget.onExpansionChanged(key,expanded);
            }
          },
          onNodeTap: (key) {

              // 回调
              if( widget.onClick !=null ){
                widget.onClick(key);
              }
              // debugPrint('Selected: $key');
              // print(key);
              // // setState(() {
              // //   _selectedNode = key;
              // //   _treeViewController =
              // //       _treeViewController.copyWith(selectedKey: key);
              // // });
          },
          theme: _treeViewTheme,
        ),
      );
      
    }
}